﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.BIM;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.MEP;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class SprinklerCalOperate
	{
		public SprinklerCalOperate()
		{
			if (!File.Exists(this.markPath))
			{
				File.Copy(Product.InstallLocation + "\\Data\\HYMarking.xml", this.markPath, true);
			}
			this.xmlDoc.Load(this.markPath);
			if ((this.xmlDoc.FirstChild.NextSibling as XmlElement).GetAttribute("version") != "BIMSpace2019")
			{
				File.Copy(Path.Combine(Product.DataLocation, "HYMarking.xml"), this.markPath, true);
				this.xmlDoc = new XmlDocument();
				this.xmlDoc.Load(this.markPath);
				XmlNode nextSibling = this.xmlDoc.FirstChild.NextSibling;
			}
			if (!File.Exists(this.dataSource))
			{
				File.Copy(Product.InstallLocation + "\\Data\\HYMEPDatasource.db", this.dataSource, true);
			}
			SQLiteDataOperate.SetDataSource(this.dataSource);
		}

		public List<PipeDiameter> ListPipeDiameters { get; set; }

		public int GetSprinklerPara(string paraName)
		{
			int result = 0;
			XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Marking").SelectSingleNode(paraName);
			if (xmlNode != null)
			{
				result = Convert.ToInt32(xmlNode.InnerText);
			}
			return result;
		}

		public void GetSprinklerMarkPara(string paraName, ref double value)
		{
			try
			{
				XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Marking").SelectSingleNode(paraName);
				if (xmlNode != null)
				{
					value = Convert.ToDouble(xmlNode.InnerText);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				value = 0.0;
			}
		}

		public void GetSprinklerMarkPara(string paraName, ref bool value)
		{
			try
			{
				XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Marking").SelectSingleNode(paraName);
				if (xmlNode != null)
				{
					value = Convert.ToBoolean(xmlNode.InnerText);
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				value = true;
			}
		}

		public void SaveSprinklerPara(string value, string paraName)
		{
			XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Marking");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode(paraName);
			if (xmlNode2 != null)
			{
				xmlNode2.InnerText = value;
			}
			else
			{
				XmlElement xmlElement = XMLOperating.AddElement(this.xmlDoc, this.markPath, "Marking", paraName);
				xmlElement.InnerText = value;
				xmlNode.AppendChild(xmlElement);
			}
			this.xmlDoc.Save(this.markPath);
		}

		public void SaveSprinklerPara(double value, string paraName)
		{
			XmlNode xmlNode = this.xmlDoc.SelectSingleNode("Marking");
			XmlNode xmlNode2 = xmlNode.SelectSingleNode(paraName);
			if (xmlNode2 != null)
			{
				xmlNode2.InnerText = value.ToString();
			}
			else
			{
				XmlElement xmlElement = XMLOperating.AddElement(this.xmlDoc, this.markPath, "Marking", paraName);
				xmlElement.InnerText = value.ToString();
				xmlNode.AppendChild(xmlElement);
			}
			this.xmlDoc.Save(this.markPath);
		}

		public List<Fire_Danger> GetListFireDanger()
		{
			List<Fire_Danger> list = new List<Fire_Danger>();
			List<Fire_Danger> result;
			try
			{
				foreach (object obj in SQLiteDataOperate.ExecQuery("select * from [Fire_Danger]").Rows)
				{
					DataRow dataRow = (DataRow)obj;
					list.Add(new Fire_Danger
					{
						Id = Convert.ToInt32(dataRow["id"]),
						DangerLevel = Convert.ToString(dataRow["wxdj"]),
						MaxDia = Convert.ToInt32(dataRow["maxdn"]),
						Distance = Convert.ToDouble(dataRow["dis"])
					});
				}
				List<string> list2 = new List<string>
				{
					"轻危险等级",
					"中危险等级I",
					"中危险等级II",
					"严重危险等级I",
					"严重危险等级II",
					"仓库危险等级I",
					"仓库危险等级II",
					"仓库危险等级III"
				};
				for (int i = 0; i < list.Count; i++)
				{
					list[i].DangerLevel = list2[i];
				}
				result = list;
			}
			catch (MissingFieldException ex)
			{
				result = null;
			}
			return result;
		}

		public Fire_PtDn GetFirePtDn(int id)
		{
			Fire_PtDn fire_PtDn = new Fire_PtDn();
			try
			{
				DataTable dataTable = SQLiteDataOperate.ExecQuery(string.Format("select * from [Fire_PtDN] where id={0}", id));
				if (dataTable != null && dataTable.Rows.Count > 0)
				{
					fire_PtDn.Id = Convert.ToInt32(dataTable.Rows[0]["id"]);
					fire_PtDn.DangerId = Convert.ToInt32(dataTable.Rows[0]["dangerid"]);
					fire_PtDn.Diameter = Convert.ToInt32(dataTable.Rows[0]["DN"]);
					fire_PtDn.SprinklerNum = Convert.ToInt32(dataTable.Rows[0]["ptnum"]);
					fire_PtDn.AddInfo = AddSprinklerDiameter.NO;
				}
			}
			catch (Exception)
			{
				fire_PtDn = null;
				throw;
			}
			return fire_PtDn;
		}

		public List<Fire_PtDn> GetListFirePtDn(int danId)
		{
			List<Fire_PtDn> list = new List<Fire_PtDn>();
			List<Fire_PtDn> result;
			try
			{
				foreach (object obj in SQLiteDataOperate.ExecQuery(string.Format("select * from [Fire_PtDN] where dangerid={0}", danId)).Rows)
				{
					DataRow dataRow = (DataRow)obj;
					list.Add(new Fire_PtDn
					{
						Id = Convert.ToInt32(dataRow["id"]),
						DangerId = Convert.ToInt32(dataRow["dangerid"]),
						Diameter = Convert.ToInt32(dataRow["DN"]),
						SprinklerNum = Convert.ToInt32(dataRow["ptnum"]),
						AddInfo = AddSprinklerDiameter.NO
					});
				}
				result = list;
			}
			catch (MissingFieldException ex)
			{
				result = null;
			}
			return result;
		}

		public bool ExecuteCmdPT(string sql)
		{
			return SQLiteDataOperate.ExecuteCommand(sql);
		}

		public void GetSprinklerKValue(string elemId, List<PipeInfo> listInfo, ref double sNum, bool beHaveKValue)
		{
			List<string> list = new List<string>();
			foreach (PipeInfo pipeInfo in listInfo)
			{
				if (pipeInfo.ParentId == elemId)
				{
					list.Add(pipeInfo.ElemId);
					Element element = pipeInfo.ENode.Element;
					if (element is FamilyInstance && element.Category.Id.IntegerValue == -2008099)
					{
						Parameter parameter = (element as FamilyInstance).Symbol.GetParameter(BuiltInParameter.RBS_FP_SPRINKLER_K_FACTOR_PARAM);
						if (parameter == null || !parameter.HasValue || !beHaveKValue)
						{
							sNum += 80.0;
						}
						else
						{
							double num = parameter.AsDouble();
							if (num <= 0.0)
							{
								sNum += 80.0;
							}
							else
							{
								sNum += num;
							}
						}
					}
				}
			}
			if (list.Count > 0)
			{
				foreach (string elemId2 in list)
				{
					this.GetSprinklerKValue(elemId2, listInfo, ref sNum, beHaveKValue);
				}
			}
		}

		public void GetSprinklerNum(string elemId, List<PipeInfo> listInfo, ref int sNum)
		{
			List<string> list = new List<string>();
			foreach (PipeInfo pipeInfo in listInfo)
			{
				if (pipeInfo.ParentId == elemId)
				{
					list.Add(pipeInfo.ElemId);
					Element element = pipeInfo.ENode.Element;
					if (element is FamilyInstance && element.Category.Id.IntegerValue == -2008099)
					{
						sNum++;
					}
				}
			}
			if (list.Count > 0)
			{
				foreach (string elemId2 in list)
				{
					this.GetSprinklerNum(elemId2, listInfo, ref sNum);
				}
			}
		}

		public void ReGetEblowTeeAndTransation(ElementNode eNode, ref List<FamilyInstance> eblowTeeList, ref List<FamilyInstance> transationList)
		{
			if (eNode == null || eNode.Element == null || !eNode.Element.IsValidObject)
			{
				return;
			}
			if (eblowTeeList == null)
			{
				eblowTeeList = new List<FamilyInstance>();
			}
			if (transationList == null)
			{
				transationList = new List<FamilyInstance>();
			}
			if (eNode.Element is FamilyInstance && (eNode.Element as FamilyInstance).MEPModel != null && (eNode.Element as FamilyInstance).MEPModel is MechanicalFitting)
			{
				FamilyInstance familyInstance = eNode.Element as FamilyInstance;
				MechanicalFitting mechanicalFitting = (eNode.Element as FamilyInstance).MEPModel as MechanicalFitting;
				YJKRevitTools.GetElementAllConnector(familyInstance);
				PartType partType = mechanicalFitting.PartType;
                if ((int)partType - 5 > 1)
				{
					if ((int)partType == 7)
					{
						transationList.Add(familyInstance);
						YJKRevitTools.SetAnnotationProportation(familyInstance, false);
					}
				}
				else
				{
					string name = familyInstance.Symbol.Family.Name;
					if (name.Contains("空白") || name.Contains("机械"))
					{
						eblowTeeList.Add(familyInstance);
						if (name.Contains("空白"))
						{
							YJKRevitTools.SetAnnotationProportation(familyInstance, true);
							goto IL_10C;
						}
					}
					YJKRevitTools.SetAnnotationProportation(familyInstance, false);
				}
			}
			IL_10C:
			this.ReGetEblowTeeAndTransation(eNode.Next, ref eblowTeeList, ref transationList);
			if (eNode.SubElementNode != null && eNode.SubElementNode.Count > 0)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.ReGetEblowTeeAndTransation(eNode2, ref eblowTeeList, ref transationList);
				}
			}
		}

		public void GetPipesFittings(ElementNode eNode, ref List<PipeInfo> listInfo, ref int id, Fire_Danger fireDanger, ref List<ChangeElements> listChangeElem, ref List<FamilyInstance> allFittings)
		{
			if (eNode == null || eNode.Element == null || !eNode.Element.IsValidObject)
			{
				return;
			}
			if (listInfo == null)
			{
				listInfo = new List<PipeInfo>();
			}
			if (listChangeElem == null)
			{
				listChangeElem = new List<ChangeElements>();
			}
			if (allFittings == null)
			{
				allFittings = new List<FamilyInstance>();
			}
			PipeInfo pipeInfo = new PipeInfo();
			if (id == 0)
			{
				pipeInfo.ParentId = "";
			}
			else
			{
				pipeInfo.ParentId = eNode.Preview.Element.Id.ToString();
			}
			id++;
			this.SetSprinklerInfo(eNode.Element, fireDanger);
			pipeInfo.ElemId = eNode.Element.Id.ToString();
			pipeInfo.ENode = eNode;
			listInfo.Add(pipeInfo);
			if (eNode.Element is FamilyInstance && (eNode.Element as FamilyInstance).MEPModel != null && (eNode.Element as FamilyInstance).MEPModel is MechanicalFitting)
			{
				ChangeElements changeElements = null;
				FamilyInstance familyInstance = null;
				Pipe pipe = null;
				FamilyInstance familyInstance2 = eNode.Element as FamilyInstance;
				MechanicalFitting mechanicalFitting = (eNode.Element as FamilyInstance).MEPModel as MechanicalFitting;
				List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(familyInstance2);
				switch ((int)mechanicalFitting.PartType)
				{
				case 5:
					allFittings.Add(familyInstance2);
					changeElements = new ChangeElements();
					using (List<Connector>.Enumerator enumerator = elementAllConnector.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Connector fittingConn = enumerator.Current;
							familyInstance = null;
							pipe = null;
							if (!changeElements.BeUperSprinker)
							{
								changeElements.BeUperSprinker = this.GetFittingConnedPipe(fittingConn, ref familyInstance, ref pipe);
							}
							else
							{
								this.GetFittingConnedPipe(fittingConn, ref familyInstance, ref pipe);
							}
							if (pipe != null)
							{
								changeElements.ListConnPipe.Add(pipe);
							}
						}
						goto IL_2ED;
					}
					break;
				case 6:
					break;
				case 7:
					goto IL_236;
				case 8:
					allFittings.Add(familyInstance2);
					changeElements = new ChangeElements();
					foreach (Connector fittingConn2 in elementAllConnector)
					{
						familyInstance = null;
						pipe = null;
						this.GetFittingConnedPipe(fittingConn2, ref familyInstance, ref pipe);
						if (pipe != null)
						{
							changeElements.ListConnPipe.Add(pipe);
						}
					}
					changeElements.BeUperSprinker = false;
					goto IL_2ED;
				default:
					goto IL_2ED;
				}
				allFittings.Add(familyInstance2);
				changeElements = new ChangeElements();
				using (List<Connector>.Enumerator enumerator = elementAllConnector.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Connector fittingConn3 = enumerator.Current;
						familyInstance = null;
						pipe = null;
						if (!changeElements.BeUperSprinker)
						{
							changeElements.BeUperSprinker = this.GetFittingConnedPipe(fittingConn3, ref familyInstance, ref pipe);
						}
						else
						{
							this.GetFittingConnedPipe(fittingConn3, ref familyInstance, ref pipe);
						}
						if (pipe != null)
						{
							changeElements.ListConnPipe.Add(pipe);
						}
					}
					goto IL_2ED;
				}
				IL_236:
				List<Pipe> listConnPipe = null;
				int num = this.CheckConnedSprinkler(familyInstance2, ref listConnPipe);
				if (num == -1)
				{
					allFittings.Add(familyInstance2);
				}
				else if (num == 1)
				{
					allFittings.Add(familyInstance2);
					changeElements = new ChangeElements();
					changeElements.BeUperSprinker = false;
					changeElements.BeTransation = true;
					changeElements.ListConnPipe = listConnPipe;
				}
				IL_2ED:
				if (changeElements != null)
				{
					listChangeElem.Add(changeElements);
				}
			}
			this.GetPipesFittings(eNode.Next, ref listInfo, ref id, fireDanger, ref listChangeElem, ref allFittings);
			if (eNode.SubElementNode != null && eNode.SubElementNode.Count > 0)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.GetPipesFittings(eNode2, ref listInfo, ref id, fireDanger, ref listChangeElem, ref allFittings);
				}
			}
		}

		public Fire_Danger GetSpecDanger(int dangerID)
		{
			DataTable dataTable = SQLiteDataOperate.ExecQuery("select * from [Fire_Danger] where id=" + dangerID);
			Fire_Danger fire_Danger = new Fire_Danger();
			foreach (object obj in dataTable.Rows)
			{
				DataRow dataRow = (DataRow)obj;
				fire_Danger.DangerLevel = Convert.ToString(dataRow["wxdj"]);
				fire_Danger.Distance = Convert.ToDouble(dataRow["dis"]);
			}
			return fire_Danger;
		}

		private void SetSprinklerInfo(Element elem, Fire_Danger fireDanger)
		{
			if (elem == null)
			{
				return;
			}
			if (elem.GetParameter("危险等级") != null)
			{
				elem.GetParameter("危险等级").Set(fireDanger.DangerLevel);
			}
			if (elem.GetParameter("喷淋半径") != null)
			{
				elem.GetParameter("喷淋半径").Set(UnitConvert.CovertToAPI(fireDanger.Distance * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
			}
		}

		public void AddElementNode(ElementNode eNode, ref List<PipeInfo> listInfo, ref int id, int dangerId, double K)
		{
			if (eNode == null)
			{
				return;
			}
			PipeInfo pipeInfo = new PipeInfo();
			if (id == 0)
			{
				pipeInfo.ParentId = "";
			}
			else
			{
				pipeInfo.ParentId = eNode.Preview.Element.Id.ToString();
			}
			id++;
			DataTable dataTable = SQLiteDataOperate.ExecQuery("select * from [Fire_Danger] where id=" + dangerId);
			Fire_Danger fire_Danger = new Fire_Danger();
			foreach (object obj in dataTable.Rows)
			{
				DataRow dataRow = (DataRow)obj;
				fire_Danger.DangerLevel = Convert.ToString(dataRow["wxdj"]);
				fire_Danger.Distance = Convert.ToDouble(dataRow["dis"]);
			}
			if (eNode.Element.GetParameter("危险等级") != null)
			{
				eNode.Element.GetParameter("危险等级").Set(fire_Danger.DangerLevel);
			}
			if (eNode.Element.GetParameter("喷淋半径") != null)
			{
				eNode.Element.GetParameter("喷淋半径").Set(UnitConvert.CovertToAPI(fire_Danger.Distance * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
			}
			pipeInfo.ElemId = eNode.Element.Id.ToString();
			pipeInfo.ENode = eNode;
			listInfo.Add(pipeInfo);
			this.AddElementNode(eNode.Next, ref listInfo, ref id, dangerId, K);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.AddElementNode(eNode2, ref listInfo, ref id, dangerId, K);
				}
			}
		}

		public void ChangeFittingDiameter(ElementNode eNode, bool beUpSprinkler, ref List<ChangeElements> changeElemList, ref List<FamilyInstance> listTransiton)
		{
			if (changeElemList == null)
			{
				changeElemList = new List<ChangeElements>();
			}
			if (listTransiton == null)
			{
				listTransiton = new List<FamilyInstance>();
			}
			if (eNode == null || eNode.Element == null || !eNode.Element.IsValidObject)
			{
				return;
			}
			if (eNode.Element is FamilyInstance && (eNode.Element as FamilyInstance).MEPModel != null && (eNode.Element as FamilyInstance).MEPModel is MechanicalFitting)
			{
				FamilyInstance familyInstance = eNode.Element as FamilyInstance;
				MechanicalFitting mechanicalFitting = (eNode.Element as FamilyInstance).MEPModel as MechanicalFitting;
				if (!beUpSprinkler)
				{
					if ((int)mechanicalFitting.PartType != 5 && (int)mechanicalFitting.PartType != 6 && (int)mechanicalFitting.PartType != 8)
					{
						goto IL_39E;
					}
					List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(familyInstance);
					IEnumerable<List<Connector>> enumerable = this.GroupConnectors(elementAllConnector);
					double num = 0.0;
					List<Connector> list = null;
					IEnumerator<List<Connector>> enumerator = enumerable.GetEnumerator();
					{
						while (enumerator.MoveNext())
						{
							List<Connector> list2 = enumerator.Current;
							num = 0.0;
							list = list2;
							foreach (Connector connector in list)
							{
								Pipe connectedPipe = this.GetConnectedPipe(connector);
								if (connectedPipe != null)
								{
									double num2 = connectedPipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
									num = ((num2 > num) ? num2 : num);
								}
							}
							if (num > 0.0)
							{
								foreach (Connector connector2 in list)
								{
									connector2.Radius = num / 2.0;
								}
							}
						}
						goto IL_39E;
					}
				}
				if ((int)mechanicalFitting.PartType == 7 && !this.BeConnedSprinkler(familyInstance))
				{
					listTransiton.Add(familyInstance);
				}
				if ((int)mechanicalFitting.PartType == 5 || (int)mechanicalFitting.PartType == 6 || (int)mechanicalFitting.PartType == 8)
				{
					listTransiton.Add(familyInstance);
				}
				Document document = eNode.Element.Document;
				List<Connector> elementAllConnector2 = YJKRevitTools.GetElementAllConnector(familyInstance);
				new List<Pipe>();
				FamilyInstance familyInstance2 = null;
				Pipe pipe = null;
				ChangeElements changeElements = null;
                switch ((int)mechanicalFitting.PartType)
				{
				case 5:
					changeElements = new ChangeElements();
					foreach (Connector fittingConn in elementAllConnector2)
					{
						familyInstance2 = null;
						pipe = null;
						if (!changeElements.BeUperSprinker)
						{
							changeElements.BeUperSprinker = this.GetFittingConnedPipe(fittingConn, ref familyInstance2, ref pipe);
						}
						else
						{
							this.GetFittingConnedPipe(fittingConn, ref familyInstance2, ref pipe);
						}
						if (pipe != null)
						{
							changeElements.ListConnPipe.Add(pipe);
						}
					}
					goto IL_391;
				case 6:
					changeElements = new ChangeElements();
					using (List<Connector>.Enumerator enumerator2 = elementAllConnector2.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Connector fittingConn2 = enumerator2.Current;
							familyInstance2 = null;
							pipe = null;
							if (!changeElements.BeUperSprinker)
							{
								changeElements.BeUperSprinker = this.GetFittingConnedPipe(fittingConn2, ref familyInstance2, ref pipe);
							}
							else
							{
								this.GetFittingConnedPipe(fittingConn2, ref familyInstance2, ref pipe);
							}
							if (pipe != null)
							{
								changeElements.ListConnPipe.Add(pipe);
							}
						}
						goto IL_391;
					}
					break;
				case 7:
					goto IL_391;
				case 8:
					break;
				default:
					goto IL_391;
				}
				changeElements = new ChangeElements();
				foreach (Connector fittingConn3 in elementAllConnector2)
				{
					familyInstance2 = null;
					pipe = null;
					this.GetFittingConnedPipe(fittingConn3, ref familyInstance2, ref pipe);
					if (pipe != null)
					{
						changeElements.ListConnPipe.Add(pipe);
					}
				}
				changeElements.BeUperSprinker = false;
				document.Regenerate();
				IL_391:
				if (changeElements != null)
				{
					changeElemList.Add(changeElements);
				}
			}
			IL_39E:
			this.ChangeFittingDiameter(eNode.Next, beUpSprinkler, ref changeElemList, ref listTransiton);
			if (eNode.SubElementNode != null && eNode.SubElementNode.Count > 0)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.ChangeFittingDiameter(eNode2, beUpSprinkler, ref changeElemList, ref listTransiton);
				}
			}
		}

		private bool BeConnedSprinkler(FamilyInstance transition)
		{
			bool result = false;
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(transition);
			if (elementAllConnector == null || elementAllConnector.Count < 1)
			{
				return result;
			}
			foreach (Connector connector in elementAllConnector)
			{
				FamilyInstance familyInstance = YJKRevitTools.GetConnectConnector(connector).Owner as FamilyInstance;
				if (familyInstance != null && this.BeSprinkler(familyInstance))
				{
					result = true;
					break;
				}
			}
			return result;
		}

		private int CheckConnedSprinkler(FamilyInstance transition, ref List<Pipe> listPipe)
		{
			listPipe = new List<Pipe>();
			int result = -1;
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(transition);
			if (elementAllConnector == null || elementAllConnector.Count != 2)
			{
				return result;
			}
			int num = 0;
			foreach (Connector connector in elementAllConnector)
			{
				Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
				if (connectConnector != null)
				{
					if (connectConnector.Owner is FamilyInstance)
					{
						FamilyInstance familyInstance = connectConnector.Owner as FamilyInstance;
						if (familyInstance != null && this.BeSprinkler(familyInstance))
						{
							result = 0;
							break;
						}
					}
					if (connectConnector.Owner is Pipe)
					{
						num++;
						listPipe.Add(connectConnector.Owner as Pipe);
					}
				}
			}
			if (num == 2)
			{
				result = 1;
			}
			return result;
		}

		private bool GetFittingConnedPipe(Connector fittingConn, ref FamilyInstance transition, ref Pipe connedPipe)
		{
			bool result = false;
			Connector connectConnector = YJKRevitTools.GetConnectConnector(fittingConn);
			if (connectConnector == null)
			{
				transition = null;
				connedPipe = null;
				return result;
			}
			Element owner = connectConnector.Owner;
			if (owner is Pipe)
			{
				transition = null;
				result = this.JudgePipeConnBeUperSprinkler(owner, ref connedPipe, connectConnector);
				connedPipe = (owner as Pipe);
			}
			else
			{
                if (!(owner is FamilyInstance) || !this.BeTransition(owner as FamilyInstance, (PartType)7))
				{
					transition = null;
					connedPipe = null;
					return result;
				}
				List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(owner);
				Connector connectConnector2 = YJKRevitTools.GetConnectConnector(this.JudgeConnectorVector(elementAllConnector[0], connectConnector) ? elementAllConnector[1] : elementAllConnector[0]);
				if (connectConnector2 == null)
				{
					transition = null;
					connedPipe = null;
					return result;
				}
				Element owner2 = connectConnector2.Owner;
				if (!(owner2 is Pipe))
				{
					transition = null;
					connedPipe = null;
					return result;
				}
				transition = (owner as FamilyInstance);
				result = this.JudgePipeConnBeUperSprinkler(owner2, ref connedPipe, connectConnector2);
				connedPipe = (owner2 as Pipe);
			}
			return result;
		}

		private bool JudgePipeConnBeUperSprinkler(Element firstElem, ref Pipe connedPipe, Connector pipeConn)
		{
			connedPipe = (firstElem as Pipe);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(connedPipe);
			Connector connectConnector = YJKRevitTools.GetConnectConnector(this.JudgeConnectorVector(hypiping.StartConnector.Connector, pipeConn) ? hypiping.EndConnector.Connector : hypiping.StartConnector.Connector);
			if (connectConnector == null)
			{
				return false;
			}
			Element owner = connectConnector.Owner;
			if (!(owner is FamilyInstance))
			{
				return false;
			}
			FamilyInstance familyInstance = owner as FamilyInstance;
			if (this.BeSprinkler(familyInstance))
			{
				return this.BeUpSprinkler(familyInstance);
			}
            return this.BeTransition(familyInstance, (PartType)7) && this.BeConnSprinkler(familyInstance);
		}

		private bool BeConnSprinkler(FamilyInstance instance)
		{
			bool result = false;
            if (!this.BeTransition(instance, (PartType)7))
			{
				return result;
			}
			foreach (Connector connector in YJKRevitTools.GetElementAllConnector(instance))
			{
				Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
				if (connectConnector != null)
				{
					Element owner = connectConnector.Owner;
					if (owner is FamilyInstance)
					{
						FamilyInstance familyInstance = owner as FamilyInstance;
						if (this.BeSprinkler(familyInstance) && this.BeUpSprinkler(familyInstance))
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}

		private bool JudgeConnectorVector(Connector conn1, Connector conn2)
		{
			return conn1.CoordinateSystem.BasisZ.IsAlmostEqualTo(conn2.CoordinateSystem.BasisZ, 0.0001);
		}

		private bool BeUpSprinkler(FamilyInstance sprinkler)
		{
			bool result = false;
			if (YJKRevitTools.GetElementConnector((Autodesk.Revit.DB.Domain)3, sprinkler).CoordinateSystem.BasisZ.IsAlmostEqualTo(new XYZ(0.0, 0.0, -1.0), 0.0001))
			{
				result = true;
			}
			return result;
		}

		private bool GetConnedPipe(Connector fittingConn, ref FamilyInstance connFitting, ref Pipe pipe)
		{
			Connector connectConnector = YJKRevitTools.GetConnectConnector(fittingConn);
			Element owner = connectConnector.Owner;
			if (owner is FamilyInstance)
			{
                if (!this.BeTransition(owner as FamilyInstance, (PartType)7))
				{
					return false;
				}
				List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(owner);
				Connector otherConnetor = this.GetOtherConnetor(elementAllConnector, connectConnector);
				if (otherConnetor == null)
				{
					return false;
				}
				Connector connectConnector2 = YJKRevitTools.GetConnectConnector(otherConnetor);
				Element owner2 = connectConnector2.Owner;
				if (!(owner2 is Pipe))
				{
					return false;
				}
				bool flag = this.JudgeAndGetPipeConn(owner2, connectConnector2) != null;
				pipe = (owner2 as Pipe);
				if (flag)
				{
					connFitting = (owner as FamilyInstance);
				}
			}
			else if (owner is Pipe)
			{
				bool flag2 = this.JudgeAndGetPipeConn(owner, connectConnector) != null;
				pipe = (owner as Pipe);
				if (!flag2)
				{
					return false;
				}
			}
			return true;
		}

		private Connector GetOtherConnetor(List<Connector> allConns, Connector connedConn)
		{
			Connector result = null;
			if (allConns == null || allConns.Count != 2)
			{
				return result;
			}
			foreach (Connector connector in allConns)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * connedConn.CoordinateSystem.BasisZ, 0.0001))
				{
					result = connector;
					break;
				}
			}
			return result;
		}

		private Connector JudgeAndGetPipeConn(Element elem, Connector connedConn)
		{
			Connector result = null;
			Pipe pipe = elem as Pipe;
			if (pipe == null)
			{
				return result;
			}
			List<Connector> list = new List<Connector>();
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(pipe);
			list.Add(hypiping.StartConnector.Connector);
			list.Add(hypiping.EndConnector.Connector);
			Connector connectConnector = YJKRevitTools.GetConnectConnector(this.GetOtherConnetor(list, connedConn));
			Element owner = connectConnector.Owner;
			if (owner == null)
			{
				return connedConn;
			}
			if (!(owner is FamilyInstance))
			{
				return result;
			}
			FamilyInstance instance = owner as FamilyInstance;
			if (this.BeSprinkler(instance))
			{
				return result;
			}
            if (this.BeTransition(instance, (PartType)7))
			{
				Element owner2 = YJKRevitTools.GetConnectConnector(this.GetOtherConnetor(YJKRevitTools.GetElementAllConnector(owner), connectConnector)).Owner;
				if (owner2 is FamilyInstance && this.BeSprinkler(owner2 as FamilyInstance))
				{
					return result;
				}
				result = connedConn;
			}
			else
			{
				result = connedConn;
			}
			return result;
		}

		private bool BeSprinkler(FamilyInstance instance)
		{
			bool result = false;
			if (instance.Category.Id.IntegerValue == -2008099)
			{
				result = true;
			}
			return result;
		}

		private bool BeTransition(FamilyInstance instance, PartType partType)
		{
			bool result = false;
			if (instance.Category.Id.IntegerValue == -2008049)
			{
				MechanicalFitting mechanicalFitting = instance.MEPModel as MechanicalFitting;
				if (mechanicalFitting != null && mechanicalFitting.PartType == partType)
				{
					result = true;
				}
			}
			return result;
		}

		private IList<List<Connector>> GroupConnectors(List<Connector> connList)
		{
			IList<List<Connector>> list = new List<List<Connector>>();
			if (connList == null || connList.Count <= 0)
			{
				return list;
			}
			for (int i = 0; i < connList.Count - 1; i++)
			{
				bool flag = false;
				List<Connector> list2 = new List<Connector>();
				for (int j = i + 1; j < connList.Count; j++)
				{
					if (connList[i].CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * connList[j].CoordinateSystem.BasisZ, 0.0001))
					{
						list2.Add(connList[i]);
						list2.Add(connList[j]);
						flag = true;
						break;
					}
				}
				if (!flag && !this.CheckContains(list, connList[i]))
				{
					list2.Add(connList[i]);
				}
				if (list2.Count > 0)
				{
					list.Add(list2);
				}
			}
			return list;
		}

		private bool CheckContains(IList<List<Connector>> groupConns, Connector conn)
		{
			bool result = false;
			if (groupConns == null || conn == null)
			{
				return result;
			}
			IEnumerator<List<Connector>> enumerator = groupConns.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Contains(conn))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private Pipe GetConnectedPipe(Connector connector)
		{
			if (connector == null)
			{
				return null;
			}
			Connector connectedConnector = connector.GetConnectedConnector();
			if (connectedConnector == null || connectedConnector.Owner == null)
			{
				return null;
			}
			if (connectedConnector.Owner is Pipe)
			{
				return connectedConnector.Owner as Pipe;
			}
			if (connectedConnector.Owner is FamilyInstance && (connectedConnector.Owner as FamilyInstance).MEPModel != null && (int)((connectedConnector.Owner as FamilyInstance).MEPModel as MechanicalFitting).PartType == 7)
			{
				foreach (object obj in (connectedConnector.Owner as FamilyInstance).MEPModel.ConnectorManager.Connectors)
				{
					Connector connector2 = (Connector)obj;
					if (connector2 != connectedConnector && !connector2.Origin.IsAlmostEqualTo(connectedConnector.Origin, 1E-06))
					{
						return this.GetConnectedPipe(connector2);
					}
				}
			}
			return null;
		}

		public IList<Element>[] GetAndChangeDiameterPipes(ElementNode eNode, List<Fire_PtDn> listPtNum, int maxDiameter, int dangerId, double K, int minDiameter, bool beHaveKValue, ref List<ChangeElements> listChangeElem, ref List<FamilyInstance> allFittings)
		{
			IList<Element>[] array = new IList<Element>[]
			{
				new List<Element>(),
				new List<Element>()
			};
			List<PipeInfo> list = new List<PipeInfo>();
			Fire_Danger specDanger = this.GetSpecDanger(dangerId);
			int num = 0;
			this.GetPipesFittings(eNode, ref list, ref num, specDanger, ref listChangeElem, ref allFittings);
			this.ListPipeDiameters = new List<PipeDiameter>();
			foreach (PipeInfo pipeInfo in list)
			{
				PipeDiameter pipeDiameter = new PipeDiameter();
				Pipe pipe = pipeInfo.ENode.Element as Pipe;
				if (pipe != null)
				{
					pipeDiameter.PipeElement = pipe;
					double kValue = 0.0;
					this.GetSprinklerKValue(pipe.Id.ToString(), list, ref kValue, beHaveKValue);
					int sprinklerNumber = this.GetSprinklerNumber(kValue);
					if (sprinklerNumber < 1)
					{
						array[0].Add(pipeInfo.ENode.Element);
					}
					else
					{
						bool bConnSprinkler = this.PipeConnSprinkler(pipe);
						int pdiameterBySprinklers = this.GetPDiameterBySprinklers(sprinklerNumber, listPtNum, maxDiameter, K, bConnSprinkler, minDiameter);
						pipeDiameter.Diameter = (double)pdiameterBySprinklers;
						this.ListPipeDiameters.Add(pipeDiameter);
						array[1].Add(pipeInfo.ENode.Element);
					}
				}
			}
			return array;
		}

		public bool ReConnPipes(Pipe pipe, List<ChangeElements> listChangeElem, int nDiameter, bool bl = false)
		{
			List<ChangeElements> list = new List<ChangeElements>();
			List<ChangeElements> list2 = new List<ChangeElements>();
			List<ChangeElements> list3 = new List<ChangeElements>();
			List<ChangeElements> list4 = new List<ChangeElements>();
			List<ChangeElements> list5 = new List<ChangeElements>();
			List<ChangeElements> list6 = new List<ChangeElements>();
			Document document = pipe.Document;
			bool result = false;
			PipeType pipeType = pipe.PipeType;
			bool beSKPipeType = pipeType.Name.Contains("丝接") && pipeType.Name.Contains("卡箍");
			int sprinklerPara = this.GetSprinklerPara("MinValue");
			double minValue = (double)((sprinklerPara > 0) ? sprinklerPara : 80);
			int sprinklerPara2 = this.GetSprinklerPara("MaxValue");
			double maxValue = (sprinklerPara2 > sprinklerPara) ? ((double)sprinklerPara2) : double.MaxValue;
			if (listChangeElem != null && listChangeElem.Count > 0)
			{
				HYPipeConnect hypipeConnect = new HYPipeConnect();
				foreach (ChangeElements changeElements in listChangeElem)
				{
					if (!changeElements.BeUperSprinker && changeElements.ListConnPipe != null && changeElements.ListConnPipe.Count > 0)
					{
						switch (changeElements.ListConnPipe.Count)
						{
						case 2:
							if (changeElements.BeTransation)
							{
								hypipeConnect.NewTransitionFitting(changeElements.ListConnPipe, 0);
							}
							else
							{
								hypipeConnect.NewElbowFitting(changeElements.ListConnPipe, false);
							}
							break;
						case 3:
							if (bl)
							{
								FamilyInstance familyInstance = hypipeConnect.NewTeeFitting(ref changeElements.ListConnPipe, changeElements.ListPoints);
								int newTeeFittingStatus = MethodFixedPipeDiameter.GetNewTeeFittingStatus(changeElements.ListConnPipe);
								if (newTeeFittingStatus == 3)
								{
									list.Add(changeElements);
									document.Delete(familyInstance.Id);
								}
								else if (newTeeFittingStatus == -1)
								{
									if (MethodFixedPipeDiameter.BeSKTreeFitting(changeElements.ListConnPipe, beSKPipeType, minValue, maxValue))
									{
										list3.Add(changeElements);
										document.Delete(familyInstance.Id);
									}
									else
									{
										hypipeConnect.NewTeeFitting(ref changeElements.ListConnPipe, false);
									}
								}
								else if (newTeeFittingStatus > 3)
								{
									list2.Add(changeElements);
									document.Delete(familyInstance.Id);
								}
							}
							else if (MethodFixedPipeDiameter.BeSKTreeFitting(changeElements.ListConnPipe, beSKPipeType, minValue, maxValue))
							{
								list3.Add(changeElements);
							}
							else
							{
								hypipeConnect.NewTeeFitting(ref changeElements.ListConnPipe, false);
							}
							break;
						case 4:
							if (bl)
							{
								int newCrossFittingStatus = MethodFixedPipeDiameter.GetNewCrossFittingStatus(changeElements.ListConnPipe);
								if (newCrossFittingStatus == 3)
								{
									list4.Add(changeElements);
								}
								else if (newCrossFittingStatus == -1)
								{
									if (MethodFixedPipeDiameter.BeSKCrossFitting(changeElements.ListConnPipe, beSKPipeType, minValue, maxValue))
									{
										list6.Add(changeElements);
									}
									else
									{
										hypipeConnect.NewCrossFitting(ref changeElements.ListConnPipe, false);
									}
								}
								else if (newCrossFittingStatus > 3)
								{
									list5.Add(changeElements);
								}
							}
							else if (MethodFixedPipeDiameter.BeSKCrossFitting(changeElements.ListConnPipe, beSKPipeType, minValue, maxValue))
							{
								list6.Add(changeElements);
							}
							else
							{
								hypipeConnect.NewCrossFitting(ref changeElements.ListConnPipe, false);
							}
							break;
						}
					}
				}
				Dictionary<int, IList<RoutingPreferenceRule>> rules = PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 1);
				document.Regenerate();
				List<FamilyInstance> list7 = new List<FamilyInstance>();
				foreach (ChangeElements changeElements2 in listChangeElem)
				{
					if (changeElements2.BeUperSprinker && changeElements2.ListConnPipe != null && changeElements2.ListConnPipe.Count > 0)
					{
						FamilyInstance familyInstance2 = null;
						switch (changeElements2.ListConnPipe.Count)
						{
						case 2:
							familyInstance2 = hypipeConnect.NewElbowFitting(changeElements2.ListConnPipe, changeElements2.ListPoints);
							break;
						case 3:
							if (bl)
							{
								FamilyInstance familyInstance3 = hypipeConnect.NewTeeFitting(ref changeElements2.ListConnPipe, changeElements2.ListPoints);
								int newTeeFittingStatus2 = MethodFixedPipeDiameter.GetNewTeeFittingStatus(changeElements2.ListConnPipe);
								if (newTeeFittingStatus2 == 3)
								{
									list.Add(changeElements2);
									document.Delete(familyInstance3.Id);
								}
								else if (newTeeFittingStatus2 == -1)
								{
									familyInstance2 = familyInstance3;
								}
								else if (newTeeFittingStatus2 > 3)
								{
									list2.Add(changeElements2);
									document.Delete(familyInstance3.Id);
								}
							}
							else
							{
								familyInstance2 = hypipeConnect.NewTeeFitting(ref changeElements2.ListConnPipe, changeElements2.ListPoints);
							}
							break;
						case 4:
							if (bl)
							{
								int newCrossFittingStatus2 = MethodFixedPipeDiameter.GetNewCrossFittingStatus(changeElements2.ListConnPipe);
								if (newCrossFittingStatus2 == 3)
								{
									list4.Add(changeElements2);
								}
								else if (newCrossFittingStatus2 != -1 && newCrossFittingStatus2 > 3)
								{
									list5.Add(changeElements2);
								}
							}
							break;
						}
						if (familyInstance2 != null)
						{
							list7.Add(familyInstance2);
						}
					}
				}
				if (list7.Count > 0)
				{
					foreach (FamilyInstance fitting in list7)
					{
						SprinklerShelter.SetFittingLenth(fitting, document, (double)nDiameter);
					}
				}
				document.Regenerate();
				try
				{
					if (list.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 32);
						document.Regenerate();
						for (int i = 0; i < list.Count; i++)
						{
							IList<Pipe> listConnPipe = list[i].ListConnPipe;
							hypipeConnect.NewTeeFitting(ref listConnPipe, list[i].ListPoints);
						}
					}
					if (list2.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 31);
						document.Regenerate();
						for (int j = 0; j < list2.Count; j++)
						{
							IList<Pipe> listConnPipe2 = list2[j].ListConnPipe;
							hypipeConnect.NewTeeFitting(ref listConnPipe2, list2[j].ListPoints);
						}
					}
					if (list4.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 42);
						document.Regenerate();
						for (int k = 0; k < list4.Count; k++)
						{
							IList<Pipe> listConnPipe3 = list4[k].ListConnPipe;
							hypipeConnect.NewCrossFitting(ref listConnPipe3, false);
						}
					}
					if (list5.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 41);
						document.Regenerate();
						for (int l = 0; l < list5.Count; l++)
						{
							IList<Pipe> listConnPipe4 = list5[l].ListConnPipe;
							hypipeConnect.NewCrossFitting(ref listConnPipe4, false);
						}
					}
					if (list3.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 53);
						document.Regenerate();
						for (int m = 0; m < list3.Count; m++)
						{
							IList<Pipe> listConnPipe5 = list3[m].ListConnPipe;
							hypipeConnect.NewTeeFitting(ref listConnPipe5, list3[m].ListPoints);
						}
					}
					if (list6.Count > 0)
					{
						PipeConfigurationManagement.GetRules(pipe.PipeType, document, false, 54);
						document.Regenerate();
						for (int n = 0; n < list6.Count; n++)
						{
							IList<Pipe> listConnPipe6 = list6[n].ListConnPipe;
							hypipeConnect.NewCrossFitting(ref listConnPipe6, false);
						}
					}
				}
				catch (Exception)
				{
				}
				if (rules != null)
				{
					PipeConfigurationManagement.SetRules(pipe.PipeType, document, rules, false);
				}
				result = true;
			}
			return result;
		}

		public ICollection<ElementId> GetAllFamilyInstance(Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			new ElementOwnerViewFilter(doc.ActiveView.Id);
			return filteredElementCollector.OfCategory(builtInCategory).OfClass(typeof(FamilyInstance)).ToElementIds();
		}

		public IList<Element>[] ChangeDiameter(ElementNode eNode, List<Fire_PtDn> listPtNum, int maxDiameter, int dangerId, double K, int minDiameter, bool beHaveKValue)
		{
			IList<Element>[] array = new IList<Element>[]
			{
				new List<Element>(),
				new List<Element>()
			};
			List<PipeInfo> list = new List<PipeInfo>();
			int num = 0;
			int num2 = 0;
			this.AddElementNode(eNode, ref list, ref num, dangerId, K);
			foreach (PipeInfo pipeInfo in list)
			{
				Pipe pipe = pipeInfo.ENode.Element as Pipe;
				if (pipe != null)
				{
					double kValue = 0.0;
					this.GetSprinklerKValue(pipe.Id.ToString(), list, ref kValue, beHaveKValue);
					int sprinklerNumber = this.GetSprinklerNumber(kValue);
					if (sprinklerNumber < 1)
					{
						array[0].Add(pipeInfo.ENode.Element);
					}
					else
					{
						bool bConnSprinkler = this.PipeConnSprinkler(pipe);
						int pdiameterBySprinklers = this.GetPDiameterBySprinklers(sprinklerNumber, listPtNum, maxDiameter, K, bConnSprinkler, minDiameter);
						if (pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).Set((double)pdiameterBySprinklers / 304.8))
						{
							num2++;
						}
						array[1].Add(pipeInfo.ENode.Element);
					}
				}
			}
			YJKMessageBox.Show("共确定并修改" + num2 + "条水管。");
			return array;
		}

		public List<Element> GetTagPipes(ElementNode eNode, int dangerId, double K, bool beHaveKValue)
		{
			List<Element> list = new List<Element>();
			List<PipeInfo> list2 = new List<PipeInfo>();
			int num = 0;
			this.AddElementNode(eNode, ref list2, ref num, dangerId, K);
			foreach (PipeInfo pipeInfo in list2)
			{
				Pipe pipe = pipeInfo.ENode.Element as Pipe;
				if (pipe != null)
				{
					double kValue = 0.0;
					this.GetSprinklerKValue(pipe.Id.ToString(), list2, ref kValue, beHaveKValue);
					if (this.GetSprinklerNumber(kValue) >= 1)
					{
						list.Add(pipeInfo.ENode.Element);
					}
				}
			}
			return list;
		}

		private int GetSprinklerNumber(double kValue)
		{
			double num = kValue / 80.0;
			int num2 = Convert.ToInt32(Math.Truncate(num));
			if (num - (double)num2 > 0.0)
			{
				num2++;
			}
			return num2;
		}

		private bool PipeConnSprinkler(Pipe pipe)
		{
			bool result = false;
			using (List<Connector>.Enumerator enumerator = YJKRevitTools.GetPipeConnConnector(pipe).GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (YJKRevitTools.GetConnectConnector(enumerator.Current).Owner.Category.Id.IntegerValue == -2008099)
					{
						result = true;
					}
				}
			}
			return result;
		}

		public int GetPDiameterBySprinklers(int spNum, List<Fire_PtDn> listPtNum, int maxDia, double K, bool bConnSprinkler, int minDiameter)
		{
			int num = 0;
			listPtNum.Sort((Fire_PtDn first, Fire_PtDn second) => first.SprinklerNum.CompareTo(second.SprinklerNum));
			int count = listPtNum.Count;
			if (spNum >= listPtNum[count - 1].SprinklerNum)
			{
				num = listPtNum[count - 1].Diameter;
			}
			else
			{
				for (int i = 0; i < count - 1; i++)
				{
					if (spNum == listPtNum[i].SprinklerNum)
					{
						num = listPtNum[i].Diameter;
					}
					else if (spNum > listPtNum[i].SprinklerNum && spNum < listPtNum[i + 1].SprinklerNum)
					{
						num = listPtNum[i + 1].Diameter;
					}
				}
			}
			if (num >= maxDia)
			{
				return maxDia;
			}
			if (K == 320.0)
			{
				if (num < 40 && !bConnSprinkler)
				{
					return 40;
				}
			}
			else if (num < 0 && minDiameter != 0 && !bConnSprinkler)
			{
				return minDiameter;
			}
			return num;
		}

		private string markPath = Path.Combine(Product.UserDataLocation, "HYMarking.xml");

		private string dataSource = Path.Combine(Product.UserDataLocation, "HYMEPDatasource.db");

		private XmlDocument xmlDoc = new XmlDocument();
	}
}
